package com.tigase.controller;

import com.tigase.bean.TigaseGroup;
import com.tigase.bean.TigaseGroupMembers;
import com.tigase.service.GroupService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by qt2 on 2017/7/21.
 */
@RestController
@RequestMapping("/group")
public class GroupController {
    @Autowired
    GroupService groupService;

    /**
     * 创建群
     * @param group
     * @param HttpServletResponse
     * @return
     */
    @RequestMapping(value = "/save")
    public Map<String,Object> insertGroup(@ModelAttribute TigaseGroup group, HttpServletResponse HttpServletResponse){
        HttpServletResponse.setHeader("Access-Control-Allow-Origin", "*");
        Map<String,Object> map = new HashMap<>();
        if(null!=group){
           TigaseGroup oGroup =groupService.getGroupJid(group.getGroupJid());
           if (oGroup==null){
               groupService.insertGroup(group);
               TigaseGroupMembers nun=new TigaseGroupMembers();
               nun.setUserType(1);
               nun.setGroupId(group.getGroupJid());
               nun.setUserId(group.getCreatName());
               nun.setInTime(new Date());
               groupService.insertGroupMember(nun);
               map.put("msg","群创建成功");
               map.put("flag",true);
           }else {
               map.put("msg","该群已存在空");
               map.put("flag",false);
           }
        }else{
            map.put("msg","参数不允许为空");
            map.put("flag",false);
        }


        return  map;

    }

    /**
     * 查找群是否存在
     * @param groupJid
     * @param HttpServletResponse
     * @return
     */
    @RequestMapping(value = "/selectGroup/{groupJid}")
    public Map<String,Object> selectGroup(@PathVariable("groupJid") String groupJid, HttpServletResponse HttpServletResponse){
        HttpServletResponse.setHeader("Access-Control-Allow-Origin", "*");
        Map<String,Object> map = new HashMap<>();
        if(null!=groupJid){
            TigaseGroup oGroup =groupService.getGroupJid(groupJid);
            if (oGroup==null){
                map.put("msg","该群不存在");
                map.put("flag",false);
            }else {
                map.put("msg","该群已存在");
                map.put("flag",true);
            }
        }else{
            map.put("msg","参数不允许为空");
            map.put("flag",false);
        }
        return  map;

    }

    /**
     * 获取群列表
     * @param
     * @param HttpServletResponse
     * @return
     */
    @RequestMapping(value = "/getGroupList/{userName}")
    public Map<String,Object> getGroupListByName(@PathVariable("userName") String userName, HttpServletResponse HttpServletResponse){
        HttpServletResponse.setHeader("Access-Control-Allow-Origin", "*");
        Map<String,Object> map=new HashMap<>();
       if(!StringUtils.isEmpty(userName)){
          List<TigaseGroup> result= groupService.selectGroupByUserId(userName);
           map.put("msg","数据获取成功");
           map.put("result",result);
           map.put("flag",true);
       }else{
           map.put("msg","参数userName不允许为空");
           map.put("flag",false);
       }
        return map;
    }

    /**
     * 获取群成员
     * @param
     * @param HttpServletResponse
     * @return
     */
    @RequestMapping(value = "/getGroupMember/{groupJid}")
    public Map<String,Object> getGroupMembers(@PathVariable("groupJid") String  groupJid,HttpServletResponse HttpServletResponse){
        HttpServletResponse.setHeader("Access-Control-Allow-Origin", "*");
        Map<String,Object> map=new HashMap<>();
        if(!StringUtils.isEmpty(groupJid)){
            List<Map<String, String>> result=groupService.findGroupMenmber(groupJid);
            map.put("msg","数据获取成功");
            map.put("result",result);
            map.put("flag",true);
        }else{
            map.put("msg","参数groupJid不允许为空");
            map.put("flag",false);
        }
        return map;
    }

    @RequestMapping(value = "/addGroupMember")
    public Map<String,Object> addGroupMember(@ModelAttribute TigaseGroupMembers user, HttpServletResponse HttpServletResponse){
        HttpServletResponse.setHeader("Access-Control-Allow-Origin", "*");
        Map<String,Object> map=new HashMap<>();
        if(null!=user){
            int result=groupService.insertGroupMember(user);
            map.put("msg","加群成功");
            map.put("flag",true);
        }else{
            map.put("msg","参数不允许为空");
            map.put("flag",false);
        }
        return map;
    }

}
